package com.ques.entity;


import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zq
 * @create 2019-11-24
 */
public class MailMessage {
    private static final String DEFAULT_SUBJECT = "无主题";
    private static final String DEFAULT_CONTENT = "无邮件内容";
    private static final String DEFAULT_FROMNAME = "未知用户";
    private static final String DEFAULT_FILENAME = "未知文件名";
    /**
     * 收件人
     */
    private List<InternetAddress> toUsers;
    /**
     * 抄送人
     */
    private List<InternetAddress> ccUsers;
    /**
     * 密送人
     */
    private List<InternetAddress> bccUsers;
    /**
     * 发件人名称
     */
    private String fromName;

    /**
     * 邮件主题
     */
    private String subject;

    /**
     * 邮件内容
     */
    private String content;
    /**
     * 发送用户的用户名验证
     */
    private String authUser;

    /**
     * 发送用户的密码验证
     */
    private String authPassword;
    /**
     * smtp邮件服务器
     */
    private String smtpHost;

    /**
     * 服务器端口号
     */
    private Integer port;

    /**
     * 是否有附件
     */
    private Boolean isEnclosure;
    /**
     * 是否有抄送 人
     */
    private Boolean isCC;
    /**
     * 是否有密送 人
     */
    private Boolean isBcc;
    /**
     * 附件列表
     */
    private List<Map<String, String>> annexs;

    public MailMessage() {
        this.isEnclosure = false;
        this.isCC = false;
        this.isBcc = false;
        this.port = 25;
    }

    /**
     * 添加附件
     * @param annex 附件路径
     * @param filename 附件名
     * @return 当前对象
     */
    public MailMessage addAunnexs(String annex, String filename) {
        if (annex == null || annex.isEmpty()) {
            return this;
        }
        if (annex == filename || filename.isEmpty()) {
            filename = DEFAULT_FILENAME;
        }
        if (annexs == null) {
            annexs = new ArrayList<>();
        }
        Map<String, String> map = new HashMap<>();
        map.put("fileName", filename);
        map.put("filePath", annex);
        annexs.add(map);
        if (annexs.size() > 0) {
            isEnclosure = true;
        }
        return this;
    }

    /**
     * 设置 邮件端 口
     * @param port 端口号
     * @return 当前对象
     */
    public MailMessage port(Integer port){
        if (port!=null) {
            this.port = port ;
        }
        return  this ;
    }
    public MailMessage to(String to) throws AddressException {
        if (judgeNull(to)) {
            return this ;
        }
        if (toUsers == null) {
            toUsers = new ArrayList<>() ;
        }
        toUsers.add(new InternetAddress(to)) ;
        return  this ;
    }
    public MailMessage bcc(String bcc) throws AddressException {
        if (judgeNull(bcc)) {
            return this;
        }
        if (bccUsers == null) {
            bccUsers = new ArrayList<>();
        }
        bccUsers.add(new InternetAddress(bcc));
        if(bccUsers.size() > 0){
            this.isBcc = true;
        }
        return this;
    }

    public MailMessage cc(String cc) throws AddressException {
        if (judgeNull(cc)) {
            return this;
        }
        if ( ccUsers== null) {
            ccUsers = new ArrayList<>();
        }
        ccUsers.add(new InternetAddress(cc));
        if(ccUsers.size() > 0){
            isCC = true;
        }
        return this;
    }

    public MailMessage subject(String subject) {
        if (judgeNull(subject)) {
            this.subject = DEFAULT_SUBJECT;
        } else {
            this.subject = subject;
        }
        return this;
    }

    public MailMessage content(String content) {
        if (judgeNull(content)) {
            this.content = DEFAULT_CONTENT;
        } else {
            this.content = content;
        }
        return this;
    }

    public MailMessage authUser(String authUser) {
        this.authUser = authUser;
        return this;
    }

    public MailMessage authPassword(String authPassword) {
        this.authPassword = authPassword;
        return this;
    }

    public MailMessage smtpHost(String smtpHost) {
        this.smtpHost = smtpHost;
        return this;
    }
    private boolean judgeNull(String str){
        return str == null || str.isEmpty() ;
    }

    public List<InternetAddress> getToUsers() {
        return toUsers;
    }

    public void setToUsers(List<InternetAddress> toUsers) {
        this.toUsers = toUsers;
    }

    public List<InternetAddress> getCcUsers() {
        return ccUsers;
    }

    public void setCcUsers(List<InternetAddress> ccUsers) {
        this.ccUsers = ccUsers;
    }

    public List<InternetAddress> getBccUsers() {
        return bccUsers;
    }

    public void setBccUsers(List<InternetAddress> bccUsers) {
        this.bccUsers = bccUsers;
    }

    public String getFromName() {
        return fromName;
    }

    public void setFromName(String fromName) {
        this.fromName = fromName;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAuthUser() {
        return authUser;
    }

    public void setAuthUser(String authUser) {
        this.authUser = authUser;
    }

    public String getAuthPassword() {
        return authPassword;
    }

    public void setAuthPassword(String authPassword) {
        this.authPassword = authPassword;
    }

    public String getSmtpHost() {
        return smtpHost;
    }

    public void setSmtpHost(String smtpHost) {
        this.smtpHost = smtpHost;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public List<Map<String, String>> getAnnexs() {
        return annexs;
    }

    public void setAnnexs(List<Map<String, String>> annexs) {
        this.annexs = annexs;
    }

    public Boolean getEnclosure() {
        return isEnclosure;
    }

    public Boolean getCC() {
        return isCC;
    }

    public Boolean getBcc() {
        return isBcc;
    }
}
